import java.util.Scanner;
public class UI {
    
    private Counter counter = new Counter();
    private Scanner scan = new Scanner(System.in);
    private boolean exitCount;
    private boolean invalidEntry;
    private String input;
    
    //attempts to run the while loop, which exits once the boolean exitcount is true
    public void turn() {
        try {
            while (!exitCount ) {
                query();
                if (!exitCount) {
                    countMessage();
                }
                invalidEntry=false;
            }
        }
        finally {
            exitMessage();
        }
    }
    
    //method which scans input
    //filters runtime error occuring when input is empty
    private void query() {
        System.out.print("Would you like to (I)ncrement or (D)ecrement count or (E)xit: ");
        input = scan.nextLine();
        if (input.length() > 0) {
            IorD(input.charAt(0));
        }
        else {
            invalidEntry=true;
        }
    }
    
    //method which receives input from query(), and then increments or decrements count
    //also used to trigger exit, by setting the boolean exitCount as true
    private void IorD (char c) {
        if (c == 'I' || c == 'i') {
            counter.incrementCount();
        }
        else if (c == 'd' || c == 'D') {
            counter.decrementCount();
        }
        else if (c == 'e' || c == 'E') {
            exitCount = true;
        }
        else {
            System.out.println("Invalid Choice");
        }
    }
    
    //simple print method to display the current count
    private void countMessage() {
        System.out.println("The count is currently: " + counter.getCount());
    }
    
    //simple print method to display the final count
    private void exitMessage() {
        System.out.println("The final count was: " + counter.getCount());
    }
}